2020-2021 Sunseeker Telemetry and Lighting System
eusci_b_i2c_ex5_masterMultipleSlave.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 #include "driverlib.h"
33 
34 //******************************************************************************
35 // Demo - EUSCI_B0 I2C Master TX bytes to Multiple Slaves
36 //
37 // Description: This demo connects two MSP430's via the I2C bus.
38 // The master transmits to 4 different I2C slave addresses 0x0A,0x0B,0x0C&0x0D.
39 // Each slave address has a specific related data in the array TXData[].
40 // At the end of four I2C transactions the slave address rolls over and begins
41 // again at 0x0A.
42 // ACLK = n/a, MCLK = SMCLK = BRCLK = default DCO = ~1.045MHz
43 //
44 // /|\ /|\
45 // MSP430FR5969 10k 10k MSP430FR5969
46 // slave | | master
47 // ----------------- | | -----------------
48 // -|XIN P1.6/UCB0SDA|<-|----+->|P1.6/UCB0SDA XIN|-
49 // | | | | |
50 // -|XOUT | | | XOUT|-
51 // | P1.7/UCB0SCL|<-+------>|P1.7/UCB0SCL |
52 // | | | |
53 //
54 //******************************************************************************
55 
56 
57 uint8_t TXData[]= {0xA1,0xB1,0xC1,0xD1};// Pointer to TX data
58 uint8_t SlaveAddress[]= {0x0A,0x0B,0x0C,0x0D};
59 uint8_t TXByteCtr;
60 uint8_t SlaveFlag = 0;
61 
62 void main(void)
63 {
64  WDT_A_hold(WDT_A_BASE);
65 
66  //Set DCO frequency to 1MHz
67  CS_setDCOFreq(CS_DCORSEL_0,CS_DCOFSEL_0);
68  //Set ACLK = VLO with frequency divider of 1
69  CS_initClockSignal(CS_ACLK,CS_VLOCLK_SELECT,CS_CLOCK_DIVIDER_1);
70  //Set SMCLK = DCO with frequency divider of 1
71  CS_initClockSignal(CS_SMCLK,CS_DCOCLK_SELECT,CS_CLOCK_DIVIDER_1);
72  //Set MCLK = DCO with frequency divider of 1
73  CS_initClockSignal(CS_MCLK,CS_DCOCLK_SELECT,CS_CLOCK_DIVIDER_1);
74 
75  // Configure Pins for I2C
76  //Set P1.6 and P1.7 as Secondary Module Function Input.
77  /*
78 
79  * Select Port 1
80  * Set Pin 6, 7 to input Secondary Module Function, (UCB0SIMO/UCB0SDA, UCB0SOMI/UCB0SCL).
81  */
82  GPIO_setAsPeripheralModuleFunctionInputPin(
83  GPIO_PORT_P1,
84  GPIO_PIN6 + GPIO_PIN7,
85  GPIO_SECONDARY_MODULE_FUNCTION
86  );
87 
88  /*
89  * Disable the GPIO power-on default high-impedance mode to activate
90  * previously configured port settings
91  */
92  PMM_unlockLPM5();
93 
94  EUSCI_B_I2C_initMasterParam param = {0};
95  param.selectClockSource = EUSCI_B_I2C_CLOCKSOURCE_SMCLK;
96  param.i2cClk = CS_getSMCLK();
97  param.dataRate = EUSCI_B_I2C_SET_DATA_RATE_400KBPS;
98  param.byteCounterThreshold = 0;
99  param.autoSTOPGeneration = EUSCI_B_I2C_NO_AUTO_STOP;
100  EUSCI_B_I2C_initMaster(EUSCI_B0_BASE, &param);
101 
102 
103  //Set Master in receive mode
104  EUSCI_B_I2C_setMode(EUSCI_B0_BASE,
105  EUSCI_B_I2C_TRANSMIT_MODE
106  );
107  //Enable I2C Module to start operations
108  EUSCI_B_I2C_enable(EUSCI_B0_BASE);
109 
110  EUSCI_B_I2C_clearInterrupt(EUSCI_B0_BASE,
111  EUSCI_B_I2C_TRANSMIT_INTERRUPT0 +
112  EUSCI_B_I2C_NAK_INTERRUPT
113  );
114 
115  //Enable master Receive interrupt
116  EUSCI_B_I2C_enableInterrupt(EUSCI_B0_BASE,
117  EUSCI_B_I2C_TRANSMIT_INTERRUPT0 +
118  EUSCI_B_I2C_NAK_INTERRUPT
119  );
120 
121  SlaveFlag = 0;
122  while(1)
123  {
124  //Specify slave address
125  EUSCI_B_I2C_setSlaveAddress(EUSCI_B0_BASE,
127  );
128 
129 
130  TXByteCtr = 1; // Load TX byte counter
131 
132  while (EUSCI_B_I2C_SENDING_STOP == EUSCI_B_I2C_masterIsStopSent
133  (EUSCI_B0_BASE));
134 
135 
136  EUSCI_B_I2C_masterSendStart(EUSCI_B0_BASE);
137 
138  __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
139  // Remain in LPM0 until all data is TX'd
140 
141  // Change Slave address
142  SlaveFlag++;
143  if(SlaveFlag > 3) // Roll over slave address
144  {
145  SlaveFlag = 0;
146  __delay_cycles(1000); // Delay between transmissions
147  }
148  }
149 }
150 
151 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
152 #pragma vector=USCI_B0_VECTOR
153 __interrupt
154 #elif defined(__GNUC__)
155 __attribute__((interrupt(USCI_B0_VECTOR)))
156 #endif
157 void USCIB0_ISR(void)
158 {
159  switch(__even_in_range(UCB0IV, USCI_I2C_UCBIT9IFG))
160  {
161  case USCI_NONE: // No interrupts break;
162  break;
163  case USCI_I2C_UCALIFG: // Arbitration lost
164  break;
165  case USCI_I2C_UCNACKIFG: // NAK received (master only)
166  // Resend START if NAK'd
167  EUSCI_B_I2C_masterSendStart(EUSCI_B0_BASE);
168  break;
169  case USCI_I2C_UCSTTIFG: // START condition detected with own address (slave mode only)
170  break;
171  case USCI_I2C_UCSTPIFG: // STOP condition detected (master & slave mode)
172  break;
173  case USCI_I2C_UCRXIFG3: // RXIFG3
174  break;
175  case USCI_I2C_UCTXIFG3: // TXIFG3
176  break;
177  case USCI_I2C_UCRXIFG2: // RXIFG2
178  break;
179  case USCI_I2C_UCTXIFG2: // TXIFG2
180  break;
181  case USCI_I2C_UCRXIFG1: // RXIFG1
182  break;
183  case USCI_I2C_UCTXIFG1: // TXIFG1
184  break;
185  case USCI_I2C_UCRXIFG0: // RXIFG0
186  break;
187  case USCI_I2C_UCTXIFG0: // TXIFG0
188  // Check TX byte counter
189  if (TXByteCtr)
190  {
191  EUSCI_B_I2C_masterSendMultiByteNext(EUSCI_B0_BASE,
192  TXData[SlaveFlag]);
193  // Decrement TX byte counter
194  TXByteCtr--;
195  }
196  else
197  {
198  EUSCI_B_I2C_masterSendMultiByteStop(EUSCI_B0_BASE);
199  // Exit LPM0
201  }
202  break;
203  case USCI_I2C_UCBCNTIFG: // Byte count limit reached (UCBxTBCNT)
204  break;
205  case USCI_I2C_UCCLTOIFG: // Clock low timeout - clock held low too long
206  break;
207  case USCI_I2C_UCBIT9IFG: // Generated on 9th bit of a transmit (for debugging)
208  break;
209  default:
210  break;
211  }
212 }
213 
MPU_initThreeSegmentsParam param
__bic_SR_register_on_exit(LPM3_bits|GIE)
__delay_cycles(500000)